
var prefix = "/server/checklog"
var almval = $('.chosen-select-almvalue').val();
var almdesc = $('#searchDesc').val();
var almlevel = $('.chosen-select-almlevel').val();
var timestampbegin = 631123200000/1000;
var timestampend = 3471264000000/1000;

$(function() {
	load();
});
function selectLoad() {
    var htmlVal = "";
    var htmlDesc = "";
    var htmlLevl = "";
    /*
    $.ajax({
        url :  prefix + "/almDesc",
        success : function(dataDesc) {
            //加载数据
            for (var i = 0; i < dataDesc.length; i++) {
                if(dataDesc[i]!=null){
                    console.log("dataDesc i:%d,lenth:%d,data:%s",i,dataDesc.length,dataDesc[i].dispdesc);
                    htmlDesc += '<option value="' + dataDesc[i].dispdesc + '">' + dataDesc[i].dispdesc + '</option>'
                }
            }
            $(".chosen-select-almdesc").append(htmlDesc);
            $(".chosen-select-almdesc").chosen({
                maxHeight : 200
            });
        }
    });
    */
    $.ajax({
        url :  prefix + "/almVal",
        success : function(dataChkVal) {
            //加载数据
            for (var i = 0; i < dataChkVal.length; i++) {
                if(dataChkVal[i]!=null){
                    console.log("dataChkVal i:%d,lenth:%d,data:%s",i,dataChkVal.length,dataChkVal[i].checkvalue);
                    htmlVal += '<option value="' + dataChkVal[i].checkvalue + '">' + dataChkVal[i].checkvalue + '</option>'
                }
            }
            $(".chosen-select-almvalue").append(htmlVal);
            $(".chosen-select-almvalue").chosen({
                maxHeight : 200
            });
        }
    });
    $.ajax({
        url :  prefix + "/almLev",
        success : function(dataAlmLev) {
            //加载数据
            for (var i = 0; i < dataAlmLev.length; i++) {
                if(dataAlmLev[i]!=null && dataAlmLev[i].alarmlevel!=""){
                    console.log("dataLev i:%d,lenth:%d,data:%s",i,dataAlmLev.length,dataAlmLev[i].alarmlevel);
                    htmlLevl += '<option value="' + dataAlmLev[i].alarmlevel + '">' + dataAlmLev[i].alarmlevel + '</option>'
                }
            }
            $(".chosen-select-almlevel").append(htmlLevl);
            $(".chosen-select-almlevel").chosen({
                maxHeight : 200
            });
        }
    });
}
function load() {
    selectLoad();
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
					//	showRefresh : true,
					//	showToggle : true,
					//	showColumns : true,
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						// queryParamsType : "limit",
						// //设置为limit则会发送符合RESTFull格式的参数
						singleSelect : false, // 设置为true将禁止多选
						// contentType : "application/x-www-form-urlencoded",
						// //发送到服务器的数据编码类型
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						//search : true, // 是否显示搜索框
						showColumns : true, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								//说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
								limit: params.limit,
								offset:params.offset,
                                checkvalue :$('.chosen-select-almvalue').val(),
                                dispdesc :$('#searchDesc').val(),
                                alarmlevel :$('.chosen-select-almlevel').val(),
                                checktimebegin :timestampbegin,
                                checktimeend :timestampend,
							};
						},
						// //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
						// queryParamsType = 'limit' ,返回参数必须包含
						// limit, offset, search, sort, order 否则, 需要包含:
						// pageSize, pageNumber, searchText, sortName,
						// sortOrder.
						// 返回false将会终止请求
						columns : [
                            {
                                field : 'checktimestamp',
                                title : '报警时间'
                            },
																{
									field : 'dispdesc', 
									title : '报警信息'
								},
																{
									field : 'checkvalue', 
									title : '报警输出'
								},
																{
									field : 'truthvalue', 
									title : '真实报警值'
								},
																{
									field : 'alarmlevel', 
									title : '报警类型'
								}
								]
					});
}
function reLoad() {
    almval = $('.chosen-select-almvalue').val();
    almdesc = $('#searchDesc').val();
    almlevel = $('.chosen-select-almlevel').val();

    var timebegin = $('#beginTime').val();
    timestampbegin =631123200000/1000;
    if(timebegin !=null && timebegin !="") {
        timebegin = timebegin.substring(0,19);
        timestampbegin = (new Date(timebegin).getTime())/1000;
    }else
        timebegin = "1990-01-01 00:00:00";
    console.log(timestampbegin);
    var timeend = $('#endTime').val();
    timestampend = 3471264000000/1000;
    if(timeend !=null && timeend !="")
    {
        timeend = timeend.substring(0,19);
        timestampend =(new Date(timeend).getTime())/1000;
    }else
        timeend = "2048-01-01 00:00:00";
    console.log(timestampend);
    var opt = {
        query : {
            checkvalue : almval,
            dispdesc : almdesc,
            alarmlevel : almlevel,
            checktimebegin :timestampbegin,
            checktimeend :timestampend,
            chktimebeginstr:timebegin,
            chktimeendstr :timeend,
        }
    }
    $('#exampleTable').bootstrapTable('refresh', opt);
}
////var rowCount = 0;
Date.prototype.format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

var g_dlfile = 0;
function downloadTable() {

    almval = $('.chosen-select-almvalue').val();
    almdesc = $('#searchDesc').val();
    almlevel = $('.chosen-select-almlevel').val();
    //开始结束时间
    var timebegin = $('#beginTime').val();
    timestampbegin =631123200000/1000;
    if(timebegin !=null && timebegin !="") {
        timebegin = timebegin.substring(0,19);
        timestampbegin = (new Date(timebegin).getTime())/1000;
    }else
        timebegin = "1990-01-01 00:00:00";
    console.log(timestampbegin);
    var timeend = $('#endTime').val();
    timestampend = 3471264000000/1000;
    if(timeend !=null && timeend !="")
    {
        timeend = timeend.substring(0,19);
        timestampend =(new Date(timeend).getTime())/1000;
    }else
        timeend = "2048-01-01 00:00:00";
    console.log(timestampend);
    var downName = "告警日志查询-"+new Date().format("yyyy-MM-dd hh:mm:ss");
    var optMax = {
        query : {
            limit: 10000000,
            offset:0,
            checkvalue : almval,
            dispdesc : almdesc,
            alarmlevel : almlevel,
            checktimebegin :timestampbegin,
            checktimeend :timestampend,
            chktimebeginstr:timebegin,
            chktimeendstr :timeend,
        }
    }
    var opt = {
        query : {
            checkvalue : almval,
            dispdesc : almdesc,
            alarmlevel : almlevel,
            checktimebegin :timestampbegin,
            checktimeend :timestampend,
            chktimebeginstr:timebegin,
            chktimeendstr :timeend,
        }
    }

    g_dlfile = 1;
    $('#exampleTable').bootstrapTable('refresh', optMax);
    $('#exampleTable').on('load-success.bs.table',function(){
        console.log("load success,gfl:%d",g_dlfile);
        if(g_dlfile == 1)
        {
            $('#exampleTable').tableExport({ type: 'excel', escape: 'false',fileName:downName,exportDataType: 'all' });
            g_dlfile = 0;
            $('#exampleTable').bootstrapTable('refresh', opt);
        }
    });


}


function add() {
	layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
}
function edit(id) {
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/edit/' + id // iframe的url
	});
}
function remove(id) {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'autoid' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

function resetPwd(id) {
}
function batchRemove() {
	var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return;
	}
	layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		var ids = new Array();
		// 遍历所有选择的行数据，取每条数据对应的ID
		$.each(rows, function(i, row) {
			ids[i] = row['autoid'];
		});
		$.ajax({
			type : 'POST',
			data : {
				"ids" : ids
			},
			url : prefix + '/batchRemove',
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					reLoad();
				} else {
					layer.msg(r.msg);
				}
			}
		});
	}, function() {

	});
}